package com.tools.cleanmaster.manager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.bean.AutoCleanFeature;
import com.tools.cleanmaster.controller.ITrashScanListener;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.TrashesData;
import com.tools.cleanmaster.model.item.TrashItem;
import com.tools.cleanmaster.utils.TrashUtils;

import java.util.List;
import java.util.Observable;

public class AutoCleanManager extends Observable {

    public static final String TAG = "AutoCleanManager";


    //入口来源
    public static final String FROM_CARD = "card";
    public static final String FROM_MENU = "menu";
    public static final String FROM_UNKNOW = "unknow";

    private final CleanApplication imContext;

    public AutoCleanManager(CleanApplication application) {
        imContext = application;
    }

    public void init() {
        if (AutoCleanFeature.AUTO_CACHE.isEnable() || AutoCleanFeature.AUTO_MEMORY.isEnable()) {
            scheduleAutoCleanTrash();
        } else {
            stopAutoCleanTask();
        }
    }

    public void scheduleAutoCleanTrash() {
        int frequency = getAutoCleanFrequency();
        Intent intent = new Intent(CleanConstant.ACTION_AUTO_CLEAN_TASK);
        PendingIntent taskPendingIntent = PendingIntent.getBroadcast(imContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) imContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(taskPendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + frequency * AlarmManager.INTERVAL_HOUR, frequency * AlarmManager.INTERVAL_HOUR, taskPendingIntent);
    }

    public void stopAutoCleanTask() {
        Intent intent = new Intent(CleanConstant.ACTION_AUTO_CLEAN_TASK);
        PendingIntent taskPendingIntent = PendingIntent.getBroadcast(imContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) imContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(taskPendingIntent);
    }

    public void doScan() {
        if (!TrashUtils.hasSdcard(TrashUtils.findSDPaths(CleanApplication.getInstance()))) {
            return;
        }
        TrashManager.getInstance().scanAppTrash(new ITrashScanListener() {
            @Override
            public void onScanStart() {

            }

            @Override
            public void onScanProgressUpdate(String desc, int percent, long size) {

            }

            @Override
            public void onScanTypeFinish(TrashType type, long size) {
                if (TrashType.APP_TRASH_FILE == type) {
                    long cleanSize = 0;
                    TrashesData data = TrashManager.getInstance().getTrashHandler(false).getTrashesData();
                    List<TrashItem> trashItems = data.getTrashesByType(TrashType.APP_TRASH_FILE);
                    if (trashItems != null) {
                        for (TrashItem item : trashItems) {
                            if (item.isSelectedDefault) {
                                item.clean();
                                cleanSize += item.size;
                            }
                        }

                        setLastCleanTotalSize(cleanSize);
                        setAutoCleanTotalSize(getAutoCleanTotalSize() + cleanSize);
                    }
                }
            }

            @Override
            public void onScanFinish() {

            }

            @Override
            public void onScanStop() {

            }
        });
    }

    public void setFeatureEnable(AutoCleanFeature feature, boolean isEnable) {
        imContext.getCleanSharedPrefManager().setAutoCleanEnable(feature, isEnable);

        imContext.getADModule().getADSharedPrefManager().setAutoCleanAdEnable(AutoCleanFeature.AUTO_CACHE.isEnable() || AutoCleanFeature.AUTO_MEMORY.isEnable());
        //应用外广告 与 自动清理广告互斥
        imContext.getADModule().getADSharedPrefManager().setAppOuterAdEnable(!imContext.getADModule().getADSharedPrefManager().getAutoCleanAdEnable());
    }

    public boolean isFeatureEnable(AutoCleanFeature feature) {
        return imContext.getCleanSharedPrefManager().isAutoCleanEnable(feature);
    }

    public void setAutoCleanFrequency(int hour) {
        imContext.getCleanSharedPrefManager().setAutoCleanFrequency(hour);
    }

    public int getAutoCleanFrequency() {
        return imContext.getCleanSharedPrefManager().getAutoCleanFrequency();
    }

    public void setAutoCleanTotalSize(long size) {
        imContext.getCleanSharedPrefManager().setAutoCleanTotalSize(size);
    }

    public long getAutoCleanTotalSize() {
        return imContext.getCleanSharedPrefManager().getAutoCleanTotalSize();
    }

    public void setLastCleanTotalSize(long size) {
        imContext.getCleanSharedPrefManager().setLastCleanTotalSize(size);
    }

    public long getLastCleanTotalSize() {
        return imContext.getCleanSharedPrefManager().getLastCleanTotalSize();
    }


}
